<?php
namespace Service\Controller;

use Think\Controller;
class RoomController extends Controller {

    public function index(){
        if(!S('smartHouse')){
            $room_db = D('Room');
            $data = $room_db->cache(true)->where(array('unit_id'=>array('neq',10237)))->relation(true)->select();
            // $data = D('Room')->query('select c.id as c_id,c.name as c_name,z.id as z_id,z.name as z_name,b.id as b_id,b.number as b_name,u.id as u_name,u.number as u_name,r.id as r_id,r.number as r_name from house_community as c,house_zone as z,house_building as b,house_unit as u,house_room as r where c.id=r.community_id AND z.id=r.zone_id AND b.id=r.building_id AND u.id=r.unit_id AND u.id <> 10237;');
        	// $this->ajaxReturn($data);
            // S('sHouse',$data);
            foreach($data as $k=>$v){
                $c[$v['community_id']]['name'] = $v['community']['name'];

                $z[$v['zone_id']]['name'] = $v['zone']['name'];
                $z[$v['zone_id']]['pid'] = $v['community_id'];

                $b[$v['building_id']]['name'] = $v['building']['number'];
                $b[$v['building_id']]['pid'] = $v['zone_id'];
                $b[$v['building_id']]['ppid'] = $v['community_id'];

                $u[$v['unit_id']]['name'] = $v['unit']['number'];
                $u[$v['unit_id']]['pid'] = $v['building_id'];
                $u[$v['unit_id']]['ppid'] = $v['zone_id'];
                $u[$v['unit_id']]['pppid'] = $v['community_id'];

                $r[$v['id']]['name'] = $v['number'];
                $r[$v['id']]['pid'] = $v['unit_id'];
                $r[$v['id']]['ppid'] = $v['building_id'];
                $r[$v['id']]['pppid'] = $v['zone_id'];
                $r[$v['id']]['ppppid'] = $v['community_id'];
            }
            // $this->ajaxReturn($c);
            // foreach($data as $k=>$v){
            //     $c[$v['c_id']]['name'] = $v['c_name'];

            //     $z[$v['z_id']]['name'] = $v['z_name'];
            //     $z[$v['z_id']]['pid'] = $v['c_id'];

            //     $b[$v['b_id']]['name'] = $v['b_name'];
            //     $b[$v['b_id']]['pid'] = $v['z_id'];
            //     $b[$v['b_id']]['ppid'] = $v['c_id'];

            //     $u[$v['u_id']]['name'] = $v['u_name'];
            //     $u[$v['u_id']]['pid'] = $v['b_id'];
            //     $u[$v['u_id']]['ppid'] = $v['z_id'];
            //     $u[$v['u_id']]['pppid'] = $v['c_id'];

            //     $r[$v['r_id']]['name'] = $v['r_name'];
            //     $r[$v['r_id']]['pid'] = $v['u_id'];
            //     $r[$v['r_id']]['ppid'] = $v['b_id'];
            //     $r[$v['r_id']]['pppid'] = $v['z_id'];
            //     $r[$v['r_id']]['ppppid'] = $v['c_id'];
            // }

            foreach($c as $k=>$v){
                $result[$k]['community_id'] = $k;
                $result[$k]['community_name'] = $v['name'];
            }

            foreach($z as $k=>$v){
                $result[$v['pid']]['data'][$k]['zone_id'] = $k;
                $result[$v['pid']]['data'][$k]['zone_name'] = $v['name'];
            }

            foreach($b as $k=>$v){
                $result[$v['ppid']]['data'][$v['pid']]['data'][$k]['building_id'] = $k;
                $result[$v['ppid']]['data'][$v['pid']]['data'][$k]['building_name'] = $v['name'];
            }

            foreach($u as $k=>$v){
                $result[$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['unit_id'] = $k;
                $result[$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['unit_name'] = $v['name'];
            }

            foreach($r as $k=>$v){
                if($result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]){
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['room_id'] = $k;
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['room_name'] = $v['name'];
                }else{
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data']['0']['unit_id'] = 0;
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data']['0']['unit_name'] = '无单元';
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data']['0']['data'][$k]['room_id'] = $k;
                    $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data']['0']['data'][$k]['room_name'] = $v['name'];
                }
            }

            S('smartHouse',$result);
        }
        $res['code'] = 200;
        $res['data'] = S('smartHouse');
		header('Access-Control-Allow-Origin: *');
        $this->ajaxReturn($res);
    }

    public function json(){
//        $roomModel = M('Room');
//        dump($roomModel);exit;
//        $data = $roomModel
//            ->join('__COMMUNITY__ ON __ROOM__.community_id = __COMMUNITY__.id','LEFT')
//            ->join('__ZONE__ ON __ROOM__.zone_id = __ZONE__.id','LEFT')
//            ->join('__BUILDING__ ON __ROOM__.building_id = __BUILDING__.id','LEFT')
//            ->join('__UNIT__ ON __ROOM__.unit_id = __UNIT__.id','LEFT')
//            ->select()
//        ;
//        S('room',$data);
//        $data = S('name');
//        dump($roomModel->getLastSql());exit;
//        dump($data[0]);exit;
//
//        $data = S('data');
//
//        $this->ajaxReturn($data[1]);

//        echo json_encode($data);

//        dump($data[1]);
//        exit;
        // dump('Public/json/'.md5('house_'.$communityId).'.json');exit;
        !I('get.force') && exit;

        $mc = M('Community');
        $mz = M('Zone');
        $mb = M('Building');
        $mu = M('Unit');
        $mr = M('Room');

        $communityId = I('get.communityId');
        $format = I('get.format') ? I('get.format') : 'json';

        $dc = $communityId ? $mc->field('id,name')->where(array('id'=>$communityId))->select() : $mc->field('id,name')->select();
        foreach($dc as $vc){
            $data[$vc['id']]['community_id'] = $vc['id'];
            $data[$vc['id']]['community_name'] = $vc['name'] ? $vc['name'] : '无小区名';
            $dz = $mz->field('id,name')->where(array('community_id'=>$vc['id']))->select();
            foreach($dz as $vz){
                $data[$vc['id']]['data'][$vz['id']]['zone_id'] = $vz['id'];
                $data[$vc['id']]['data'][$vz['id']]['zone_name'] = $vz['name'] ? $vz['name'] : '无园区名';
                $db = $mb->field('id,name')->where(array('zone_id'=>$vz['id']))->select();
                foreach($db as $vb){
                    $data[$vc['id']]['data'][$vz['id']]['data'][$vb['id']]['building_id'] = $vb['id'];
                    $data[$vc['id']]['data'][$vz['id']]['data'][$vb['id']]['building_name'] = $vb['name'] ? $vb['name'] : '无幢名';
                    $du = $mu->field('id,name')->where(array('building_id'=>$vb['id']))->select();
                    foreach($du as $vu){
                        $data[$vc['id']]['data'][$vz['id']]['data'][$vb['id']]['data'][$vu['id']]['unit_id'] = $vu['id'];
                        $data[$vc['id']]['data'][$vz['id']]['data'][$vb['id']]['data'][$vu['id']]['unit_name'] = $vu['name'] ? $vu['name'] : '无单元名';
                        $dr = $mr->field('id,name')->where(array('unit_id'=>$vu['id']))->select();
                        foreach($dr as $vr){
                            $data[$vc['id']]['data'][$vz['id']]['data'][$vb['id']]['data'][$vu['id']]['data'][$vr['id']]['room_id'] = $vr['id'];
                            $data[$vc['id']]['data'][$vz['id']]['data'][$vb['id']]['data'][$vu['id']]['data'][$vr['id']]['room_name'] = $vr['name'] ? $vr['name'] : '无房间名';
                        }
                    }
                }
            }
        }

        if($format == 'js'){
            if($communityId){
                isset($data) && $data && $this->fs('Public/json/'.md5('house_'.$communityId).'.js','window.roomData = '.json_encode(array('code'=>200,'data'=>$data)));
//                isset($data) && $data && file_put_contents('Public/json/'.md5('house_'.$communityId).'.js','window.roomData = '.json_encode(array('code'=>200,'data'=>$data)));
            }else{
                isset($data) && $data && $this->fs('Public/json/'.md5('house').'.js','window.roomData = '.json_encode(array('code'=>200,'data'=>$data)));
//                isset($data) && $data && file_put_contents('Public/json/'.md5('house').'.js','window.roomData = '.json_encode(array('code'=>200,'data'=>$data)));
            }
        }else{
            if($communityId){
                isset($data) && $data && $this->fs('Public/json/'.md5('house_'.$communityId).'.'.$format,json_encode(array('code'=>200,'data'=>$data)));
//                isset($data) && $data && file_put_contents('Public/json/'.md5('house_'.$communityId).'.'.$format,json_encode(array('code'=>200,'data'=>$data)));
            }else{
                isset($data) && $data && $this->fs('Public/json/'.md5('house').'.'.$format,json_encode(array('code'=>200,'data'=>$data)));
//                isset($data) && $data && file_put_contents('Public/json/'.md5('house').'.'.$format,json_encode(array('code'=>200,'data'=>$data)));
            }
        }
    }

    protected function fs($filePath,$content){
        $fp = fopen($filePath, 'w');
        fwrite($fp, $content);
        fclose($fp);
    }
}